﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Copy4Backup
{
	/// <summary>
	/// Folder filter.
	/// </summary>
	public class FolderFilter
	{

		#region Fields

		/// <summary>
		/// The folder filter pattern.
		/// </summary>
		private const String FolderFilterPattern = "(\\\\{0}\\\\)|(\\\\{0}$)";

		/// <summary>
		/// The filter
		/// </summary>
		private readonly String filter_;

		private readonly Regex reg_;
		#endregion Fields

		#region Properties
		#endregion Properties

		#region Methods

		/// <summary>
		/// Prevents a default instance of the FolderFilter class from being created.
		/// </summary>
		private FolderFilter()
		{
		}

		/// <summary>
		/// Initializes a new instance of the FolderFilter class.
		/// </summary>
		/// <param name="pattern">Folder filter pattern</param>
		public FolderFilter(String filter)
		{
			filter_ = filter;

			string ttt = String.Format(FolderFilterPattern, filter_);
			try
			{
				reg_ = new Regex(ttt, RegexOptions.IgnoreCase);
			}

			catch (ArgumentException excp)
			{
				System.Diagnostics.Debug.WriteLine(String.Format("Regular expression parsing error: {0}", 
					excp.Message));
			}
		}

		/// <summary>
		/// Checks, if the folder name contains specified in the ctor filter text.
		/// </summary>
		/// <param name="folder">The folder name to check.</param>
		/// <returns>True, if the folder name contains specified in the ctor filter text.</returns>
		public bool Matched(String folder)
		{
			return reg_.Match(folder).Success;
		}
		#endregion Methods
	}
}
